<?php
// $Id: CreateController.php 644 2013-01-20 10:21:08Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

class CreateController extends MemberController
{
    //在中间加了一层
    public function indexAction() {
        if($this->checkBusiness() && $this->userInfo['shopid'])
            echo 'OK';
        else
            $this->render('m_create');
    }

    public function saveAction() {

        if(!get_request('submitform')){
            header('Location: /account.php?m=biz&create');
            exit;
        }

        $error = $this->check();
        if($error){
            $this->setVar('error', $error);
            $this->setVar('data', get_request());
            $this->render('m_create');
        }else{
            $this->shopManager = Base::getObject('service.manager.shopManager');
            $this->shopManager
                 ->setField('name', get_request('name'))
                 ->setField('tags', '')
                 ->setField('create_dt', TIMESTAMP)
                 ->setField('update_dt', TIMESTAMP)
                 ->setField('owner_uid', $this->userInfo['uid'])
                 ->setField('create_uid', $this->userInfo['uid'])
                 ->setField('update_uid', $this->userInfo['uid'])
                 ->insertDb();
            $this->shopId = Db::insertId();

            $this->shopManager->reset()
                 ->setShopId($this->shopId)
                 ->setField('contact', get_request('contact'))
                 ->setField('mobile', get_request('mobile'))
                 ->setField('memo', '')
                 ->insertDb();

            header('Location: /account.php?');
            exit;
        }
    }

    private function check() {
        $name    = get_request('name');
        $contact = get_request('contact');
        $mobile  = get_request('mobile');
        $verify  = get_request('verify');
        $error   = array();

        if(empty($name)) {
            $error['name'] = '店铺名称不能为空';
        }else if(iconv_strlen($name, 'UTF-8') < 4 || iconv_strlen($name, 'UTF-8') > 20) {
            $error['name'] = '店铺名称长度为4-20个汉字';
        }

        if(empty($contact)) {
            $error['contact'] = '联系人不能为空';
        }else if(!Validate::isChinese($contact)) {
            $error['contact'] = '联系人必须为中文';
        }

        if(empty($mobile)) {
            $error['mobile'] = '手机号码不能为空';
        }else if(!Validate::isMobile($mobile)){
            $error['mobile'] = '手机号码格式错误';
        }

        if(empty($verify)) {
            $error['verify'] = '手机验证码不能为空';
        //}else if(!Validate::isMobile($mobile)){
        //    $error['verify'] = '手机验证码错误';
        }
        return $error;
    }
}
